from game import zz_game
from zz import zz_log, zz_img

log = zz_log.of()


# 寻道大千

class Game(zz_game.ZZ_Game):
    def __init__(self, d):
        super().__init__(d)

    def get_name(self):
        return "灵魂序章"

    def play_auto(self):
        """
        开启自动锤
        :return:
        """
        self.log_msg("点击锤子")
        self.img_click("./game/img/灵魂序章/play.png", 5, 2)
        self.play_alert()
        self.play_alert()

        self.img_click_if_exists("点击自动", "./game/img/灵魂序章/auto.png", "./game/img/灵魂序章/start.png")
        self.log_msg("点击开始")
        self.img_click("./game/img/灵魂序章/start.png")

    def play_alert(self):
        if not self.img_exit("./game/img/灵魂序章/play_alert.png", 2):
            self.log_msg("不存在弹窗")
            return False

        if self.img_exit("./game/img/灵魂序章/play_state.png", 3):
            self.log_msg("战力下降，点击出售")
            self.img_click("./game/img/灵魂序章/play_buy.png")
            # self.img_click("./game/img/灵魂序章/play_buy.png")
        else:
            self.log_msg("战力上升，点击收服")
            self.img_click("./game/img/灵魂序章/play_get.png")

        if self.img_exit("./game/img/灵魂序章/play_alert01.png", 1):
            self.log_msg("存在弹窗确认，点击确认")
            self.img_click("./game/img/灵魂序章/play_confirm.png")
        return True

    def game_alert(self):
        self.alert_close_by_file("./game/img/灵魂序章", 0.9)

    def handle_main(self):
        self.log_msg("等待进入登陆页面")
        self.img_exit("./game/img/灵魂序章/game_go_main.png", 100)
        self.log_msg("已进入登陆页面")
        self.d.sleep(2)
        self.game_alert()

        self.log_msg("点击开始游戏")
        self.img_click("./game/img/灵魂序章/go.png")
        self.log_msg("等待进去游戏主界面")
        self.img_exit("./game/img/灵魂序章/boss01.png", 100)
        self.log_msg("检查是否存在弹窗")

        self.game_alert()
        self.play_alert()

        self.img_click_if_exists('挑战魂兽，点击挑战', "./game/img/灵魂序章/boss01.png", "./game/img/灵魂序章/boss02.png")
        self.log_msg("点击魂兽森林")
        self.img_click("./game/img/灵魂序章/boss02.png")
        self.log_msg("点击免费")
        if self.img_click("./game/img/灵魂序章/boss03.png", 5):
            self.click_bottom()
        self.click_bottom()
        self.log_msg("挑战魂兽-完成")

        self.d.sleep(1)
        self.play_auto()

        index = 0
        while True:
            if self.check_done():
                self.log_msg("任务已完成")
                break

            if index % 3 == 0:
                self.play_auto()
                self.game_alert()

            self.log_msg(f"{index}-继续锤")
            index += 1
            self.d.sleep(20)

        self.d.sleep(2)
        # self.get_fu()
        return True
